<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page import="domain.entities.interfaces.*"%>
<%@ page import="domain.dao.interfaces.*"%>
<%@ page import="util.*"%>
<%@ page import="java.util.List"%>
<%@ page import="org.springframework.context.ApplicationContext"%>
<%@ page import="org.springframework.context.support.ClassPathXmlApplicationContext"%>
<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html"%>
<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"%>
<%@ taglib prefix="nested" uri="http://struts.apache.org/tags-nested"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Show All Contact Groups</title>
<link rel="stylesheet" type="text/css" href="/mdoc-project/css/bootstrap_Boris.css">
<base href="<%=request.getScheme()%>://<%=request.getServerName()%>:<%=request.getServerPort()%><%=request.getContextPath()%>/">
</head>
<body>

	<%
		boolean connected = session
				.getAttribute(DomainUtils.CURRENT_USER_STRING) != null;

		if (connected == false) {
			request.setAttribute(DomainUtils.ALERT_ERROR_STRING,
					"You must be connected");
		}
	%>

	<%@include file="/pages/common_style/header.jsp"%>
	<%@include file="/pages/common_style/alerts.jsp"%>

	<%
		if (connected) {
	%>

	<html:errors />
	<br>
	<html:form action="/SearchContactGroup">
		<div class="form-group">
			<label for="part_of_groupname">Search (Enter a part of the group name you're looking for)</label>
			<input type="text" class="form-control" id="part_of_groupname" value="<%=DomainUtils.getNotNullValue(request,
							"part_of_groupname")%>" placeholder="Enter a part a the group name" name="part_of_groupname">
			<br>
			<button type="submit" class="btn btn-default">Submit</button>
			<button type="reset" class="btn btn-default">Reset</button>
		</div>
	</html:form>
	<br>
	<br>

	<%
		ApplicationContext context = util.SpringUtils
					.getApplicationContext();
			IUser currentUser = (IUser) session
					.getAttribute(DomainUtils.CURRENT_USER_STRING);
			List<IContactGroup> contactGroups = (List<IContactGroup>) request
					.getAttribute(DomainUtils.SEARCH_CONTACT_GROUP_RESULT);

			IDAOContactGroup daoContactGroup = (IDAOContactGroup) context
					.getBean("beanDAOContactGroup");

			if (contactGroups == null) {
				contactGroups = daoContactGroup.getByUserId(currentUser
						.getId());
			}

			/* we need to save current contact groups because, in case of an
			   update, we don't know yet on witch we going to operate and 
			   we can't get it from database because of conccurency modification 
			   purpose. */
			String currentGroupsVersions = "/";

			for (IContactGroup cg : contactGroups) {
				currentGroupsVersions += cg.getGroupId() + ";"
						+ cg.getVersion() + "/";
			}
	%>

	<html:form action="/ActionGroupDispatcher">
		<label for="selected_contactgroup">Choose a contact group among those found</label>
		<select class="form-control" name="selected_contactgroup" id="selected_contactgroup">
			<%
				for (IContactGroup cg : contactGroups) {
			%>
			<option value="<%=cg.getGroupId()%>"><%=cg.getGroupName()%></option>
			<%
				}
			%>
		</select>
		<br>
		<label for="wanted_action_radios">Select the wanted action</label>
		<div class="form-group" id="wanted_action_radios">
			<input type="radio" name="wanted_action" id="show_details_contactgroup" value="show_details_contactgroup">
			<span class="label label-default">
				<label for="show_details_contactgroup">Show details</label>
			</span>
			<br>
			<input type="radio" name="wanted_action" id="modify_contactgroup" value="modify_contactgroup">
			<span class="label label-primary">
				<label for="modify_contactgroup">Modify group name</label>
			</span>
			<span style="padding-left: 10px">
				<input type="text" id="new_group_name" name="new_group_name" value="<%=DomainUtils.getNotNullValue(request,
							"new_group_name")%>" style="width: 200px;" placeholder="Enter here the new name of the group">
			</span>
			<br>
			<input type="radio" name="wanted_action" id="delete_contactgroup" value="delete_contactgroup">
			<span class="label label-default">
				<label for="delete_contactgroup">Delete</label>
			</span>
			<br>
			<input type="radio" name="wanted_action" id="add_contact" value="add_contact">
			<span class="label label-primary">
				<label for="add_contact">Add a contact</label>
			</span>
			<br>
			<input type="radio" name="wanted_action" id="remove_contact" value="remove_contact">
			<span class="label label-default">
				<label for="remove_contact">Remove a contact</label>
			</span>
			<br>
		</div>
		<input type="hidden" name="current_groups_versions" value="<%=currentGroupsVersions%>" />

		<br>
		<button type="submit" class="btn btn-default">Submit</button>
		<button type="reset" class="btn btn-default">Reset</button>
	</html:form>
	<% } %>

	<%@include file="/pages/common_style/footer.jsp"%>

</body>
</html>